<template>
  <div v-if="sureAdmin">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>员工评价</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="6" :offset="17">
          <el-date-picker
            v-model="date"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            @change="dateChange"
            :clearable="false"
          >
          </el-date-picker>
        </el-col>
      </el-row>

      <!-- 员工列表区域 -->
      <el-table :data="staffList" border stripe>
        <!-- <el-table-column type="index"></el-table-column> -->
        <el-table-column label="员工姓名" prop="name"></el-table-column>
        <el-table-column label="平均评分" >
          <template slot-scope="scope"> {{ (scope.row.sum/scope.row.num).toFixed(2) }}分 </template>
        </el-table-column>
        <el-table-column label="评价人数" prop="num">
          <template slot-scope="scope"> {{ scope.row.num }}人 </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取员工列表的参数对象
      queryInfo: {
        // 当前的页数
        pageNo: 1,
        // 当前每页显示多少条数据
        pageSize: 5,
        start: undefined,
        end: undefined,
      },
      //模糊查询
      query: "",
      staffList: [],
      total: 0,
      sureAdmin: false,
      date: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(end.getTime() + 3600 * 1000 * 24);
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
              // console.log(this.date);
            },
          },
        ],
      },
    };
  },
  created() {
    var Admin = window.localStorage.getItem("admin");
    console.log(this.sureAdmin, "=======================");
    if (Admin == 1) {
      this.sureAdmin = true;
      this.getDate();
      //获取员工列表
      this.getStaffEvaluate();
      // console.log((13/3).toFixed(2));
    } else {
      this.sureAdmin = false;
    }
  },
  methods: {
    dateChange() {
      this.queryInfo.start = this.date[0];
      this.queryInfo.end = this.date[1];
      this.getStaffEvaluate();
    },
    getDate() {
      const end = new Date();
      end.setTime(end.getTime() + 3600 * 1000 * 24);
      this.queryInfo.end = [
        end.getFullYear(),
        end.getMonth() + 1,
        end.getDate(),
      ].join("-");
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      this.queryInfo.start = [
        start.getFullYear(),
        start.getMonth() + 1,
        start.getDate(),
      ].join("-");
      var time = [];
      time[0] = this.queryInfo.start;
      time[1] = this.queryInfo.end;
      this.date = time;
    },
    //获取员工列表(调用后台接口)
    async getStaffEvaluate() {
      const { data: res } = await this.$http.get("comment/listCommentVO", {
        params: this.queryInfo,
      });
      console.log(res);
      if (res.code !== 200) {
        return this.$message.error("获取员工评分失败！");
      }
      this.total = res.data.count;
      this.staffList = res.data.commentVOS;
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getStaffEvaluate();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNo = newPage;
      this.getStaffEvaluate();
    },
  },
};
</script>

<style lang="less" scoped>
.button:hover {
  background-color: rgb(64, 158, 255);
  color: rgb(255, 255, 255);
}
</style>
